using BestHTTP;
using com.shinian.fcg.fbs;
using Com.Shinian.FcgProtocol.Pb;
using FlatBuffers;
using GF.Debug;
using pEventBus;
using SA.Common;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using FCGGameConfig = com.shinian.fcg.fbs.GameConfig;

public class StaticDataUtil : MonoSingleton<StaticDataUtil>
{
    string serverListUrl1 = "http://cdn-10019666.cossh.myqcloud.com/static_model/fcg/service_list";
    string serverListUrl2 = "http://cdn-10019666.file.myqcloud.com/static_model/fcg/service_list";
    int retryCount = 0;
    int loadedCount = 6;
    bool isCall = false;
    bool fileExits = false;
    string directoryPath = "";
    public BaseEnumConfig baseEnumConfig = default(BaseEnumConfig);
    public StatusCodeConfig statusCodeConfig = default(StatusCodeConfig);
    public FCGGameConfig gameConfig = default(FCGGameConfig);
    public ServerListConfig serverListConfig = default(ServerListConfig);
    FootballerList_FBS footballerList = default(FootballerList_FBS);
    SkillList_FBS skillList = default(SkillList_FBS);

    public Dictionary<int, Footballer> footballerCache = new Dictionary<int, Footballer>(); //key uid
    public Dictionary<int, Skill> skillCache = new Dictionary<int, Skill>();

    public Dictionary<int, HashSet<int>> shortPassPos = new Dictionary<int, HashSet<int>>();
    public Dictionary<int, HashSet<int>> longPassPos = new Dictionary<int, HashSet<int>>();
    public HashSet<int> attackStartPos = new HashSet<int>();
    public HashSet<int> attackShootPos = new HashSet<int>();
    public Dictionary<int, int> posOppPos = new Dictionary<int, int>();
    public ServiceListDTO serverListDTO = null;

    public static string serverListUrl = "http://cdn-10019666.cossh.myqcloud.com/static_model/fcg/service_list_test";
    void Start()
    {
        directoryPath = Application.streamingAssetsPath + "/staticdata/";
        serverListUrl = jointServerListUrl(serverListUrl1);
        FetchServerList();
    }

    string jointServerListUrl(string source)
    {
#if LCS
        if (GameMain.Instance.GameMode == GameMode.DEV || GameMain.Instance.GameMode == GameMode.QA)
        {
            return serverListUrl1 + "_test";
        }
        else {
            return serverListUrl1;
        }
#else
        return serverListUrl1 + "_test";
#endif
    }

    void FetchServerList()
    {
        fileExits = FileSystem.IsDirectoryExist(directoryPath);
        Debugger.Log("加载服务器列表:" + serverListUrl);
        HTTPRequest req0 = new HTTPRequest(new Uri(serverListUrl + "?p=" + DateTime.UtcNow.Ticks), HTTPMethods.Get, (endReq0, endResp0) =>
        {
            switch (endReq0.State)
            {
                case HTTPRequestStates.Finished:
                    retryCount = 0;
                    if (endResp0.StatusCode == 200)
                    {
                        serverListDTO = ServiceListDTO.Parser.ParseFrom(endResp0.Data);
                        if (serverListDTO.GameStatus == (int)GameServiceStatus.Open)
                        {
                            Debugger.Log("加载静态数据......");
                            string version = "";
                            for (int i = 0; i < serverListDTO.Version.Count; i++)
                            {
                                version += serverListDTO.Version[i] + ".";
                            }
                            version = version.Substring(0, version.Length - 1);
#if DevelopVersion
                            version = "0.6";
#endif
                            string staticdataBaseUrl = "http://cdn-10019666.file.myqcloud.com/static_model/fcg/" + version + "/";
                            string dataFileListUrl = staticdataBaseUrl + "data_file_list";
                            Debugger.Log("dataFileListUrl:" + dataFileListUrl);
                            HTTPRequest req1 = new HTTPRequest(new Uri(dataFileListUrl + "?p=" + DateTime.UtcNow.Ticks), HTTPMethods.Get, (req, resp) =>
                            {
                                string fileListDataText = Encoding.UTF8.GetString(req.Response.Data);
                                string[] fileList1 = fileListDataText.Split(',');
                                for (int ii = 0; ii < fileList1.Length; ++ii)
                                {
                                    string[] t = fileList1[ii].Split('|');  //文件|md5
                                    string fileName = t[0].Substring(fileList1[ii].LastIndexOf("/") + 1);
                                    Debugger.Log("data url:" + staticdataBaseUrl + t[0] + ",md5:" + t[1]);
                                    Uri dataUrl = new Uri(staticdataBaseUrl + t[0] + "?p=" + DateTime.UtcNow.Ticks);
                                    if (fileName == "BaseEnumConfig.flat")
                                    {
                                        if (fileExits)
                                        {
                                            var filePathWithName = directoryPath + fileName;
                                            var flat = FileSystem.ReadFile(filePathWithName);
                                            if (flat != null)
                                            {
                                                var md5 = FileSystem.CalcMd5Hash(flat);
                                                if (md5.Equals(t[1]))
                                                {
                                                    byte[] raw = new byte[flat.Length];
                                                    Buffer.BlockCopy(flat, 0, raw, 0, flat.Length);
                                                    SA.Common.FileSystem.EasyCrypt(ref raw);
                                                    byte[] buffer = new byte[raw.Length * 4];
                                                    int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                    byte[] decrypted = new byte[len];
                                                    Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                    baseEnumConfig = BaseEnumConfig.GetRootAsBaseEnumConfig(new ByteBuffer(decrypted));
                                                    loadedCount -= 1;
                                                    continue;
                                                }
                                            }
                                        }

                                        HTTPRequest req2 = new HTTPRequest(dataUrl, HTTPMethods.Get, (req3, resp3) =>
                                            {
                                                byte[] data = resp3.Data;
                                                byte[] raw = new byte[data.Length];
                                                Buffer.BlockCopy(data, 0, raw, 0, data.Length);
                                                SA.Common.FileSystem.EasyCrypt(ref raw);
                                                byte[] buffer = new byte[raw.Length * 4];
                                                int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                byte[] decrypted = new byte[len];
                                                Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                baseEnumConfig = BaseEnumConfig.GetRootAsBaseEnumConfig(new ByteBuffer(decrypted));
                                                loadedCount -= 1;
                                            });
                                        req2.Timeout = TimeSpan.FromMilliseconds(3000);
                                        req2.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                                        req2.DisableCache = true;
                                        req2.Send();
                                    }
                                    else if (fileName == "StatusCodeConfig.flat")
                                    {
                                        if (fileExits)
                                        {
                                            var filePathWithName = directoryPath + fileName;
                                            var flat = FileSystem.ReadFile(filePathWithName);
                                            if (flat != null)
                                            {
                                                var md5 = FileSystem.CalcMd5Hash(flat);
                                                if (md5.Equals(t[1]))
                                                {
                                                    byte[] raw = new byte[flat.Length];
                                                    Buffer.BlockCopy(flat, 0, raw, 0, flat.Length);
                                                    SA.Common.FileSystem.EasyCrypt(ref raw);
                                                    byte[] buffer = new byte[raw.Length * 4];
                                                    int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                    byte[] decrypted = new byte[len];
                                                    Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                    statusCodeConfig = StatusCodeConfig.GetRootAsStatusCodeConfig(new ByteBuffer(decrypted));
                                                    loadedCount -= 1;
                                                    continue;
                                                }
                                            }
                                        }
                                        HTTPRequest req2 = new HTTPRequest(dataUrl, HTTPMethods.Get, (req3, resp3) =>
                                        {
                                            byte[] data = resp3.Data;
                                            byte[] raw = new byte[data.Length];
                                            Buffer.BlockCopy(data, 0, raw, 0, data.Length);
                                            SA.Common.FileSystem.EasyCrypt(ref raw);
                                            byte[] buffer = new byte[raw.Length * 4];
                                            int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                            byte[] decrypted = new byte[len];
                                            Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                            statusCodeConfig = StatusCodeConfig.GetRootAsStatusCodeConfig(new ByteBuffer(decrypted));
                                            loadedCount -= 1;
                                        });
                                        req2.Timeout = TimeSpan.FromMilliseconds(3000);
                                        req2.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                                        req2.DisableCache = true;
                                        req2.Send();
                                    }
                                    else if (fileName == "GameConfig.flat")
                                    {
                                        if (fileExits)
                                        {
                                            var filePathWithName = directoryPath + fileName;
                                            var flat = FileSystem.ReadFile(filePathWithName);
                                            if (flat != null)
                                            {
                                                var md5 = FileSystem.CalcMd5Hash(flat);
                                                if (md5.Equals(t[1]))
                                                {
                                                    byte[] raw = new byte[flat.Length];
                                                    Buffer.BlockCopy(flat, 0, raw, 0, flat.Length);
                                                    SA.Common.FileSystem.EasyCrypt(ref raw);
                                                    byte[] buffer = new byte[raw.Length * 4];
                                                    int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                    byte[] decrypted = new byte[len];
                                                    Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                    gameConfig = FCGGameConfig.GetRootAsGameConfig(new ByteBuffer(decrypted));
                                                    loadedCount -= 1;
                                                    continue;
                                                }
                                            }
                                        }
                                        HTTPRequest req2 = new HTTPRequest(dataUrl, HTTPMethods.Get, (req3, resp3) =>
                                        {
                                            byte[] data = resp3.Data;
                                            byte[] raw = new byte[data.Length];
                                            Buffer.BlockCopy(data, 0, raw, 0, data.Length);
                                            SA.Common.FileSystem.EasyCrypt(ref raw);
                                            byte[] buffer = new byte[raw.Length * 4];
                                            int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                            byte[] decrypted = new byte[len];
                                            Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                            gameConfig = FCGGameConfig.GetRootAsGameConfig(new ByteBuffer(decrypted));
                                            loadedCount -= 1;
                                        });
                                        req2.Timeout = TimeSpan.FromMilliseconds(3000);
                                        req2.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                                        req2.DisableCache = true;
                                        req2.Send();
                                    }
                                    else if (fileName == "ServerListConfig.flat")
                                    {
                                        if (fileExits)
                                        {
                                            var filePathWithName = directoryPath + fileName;
                                            var flat = FileSystem.ReadFile(filePathWithName);
                                            if (flat != null)
                                            {
                                                var md5 = FileSystem.CalcMd5Hash(flat);
                                                if (md5.Equals(t[1]))
                                                {
                                                    byte[] raw = new byte[flat.Length];
                                                    Buffer.BlockCopy(flat, 0, raw, 0, flat.Length);
                                                    SA.Common.FileSystem.EasyCrypt(ref raw);
                                                    byte[] buffer = new byte[raw.Length * 4];
                                                    int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                    byte[] decrypted = new byte[len];
                                                    Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                    serverListConfig = ServerListConfig.GetRootAsServerListConfig(new ByteBuffer(decrypted));
                                                    loadedCount -= 1;
                                                    continue;
                                                }
                                            }
                                        }
                                        HTTPRequest req2 = new HTTPRequest(dataUrl, HTTPMethods.Get, (req3, resp3) =>
                                        {
                                            byte[] data = resp3.Data;
                                            byte[] raw = new byte[data.Length];
                                            Buffer.BlockCopy(data, 0, raw, 0, data.Length);
                                            SA.Common.FileSystem.EasyCrypt(ref raw);
                                            byte[] buffer = new byte[raw.Length * 4];
                                            int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                            byte[] decrypted = new byte[len];
                                            Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                            serverListConfig = ServerListConfig.GetRootAsServerListConfig(new ByteBuffer(decrypted));
                                            loadedCount -= 1;
                                        });
                                        req2.Timeout = TimeSpan.FromMilliseconds(3000);
                                        req2.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                                        req2.DisableCache = true;
                                        req2.Send();
                                    }
                                    else if (fileName == "FootballerList_FBS.flat")
                                    {
                                        if (fileExits)
                                        {
                                            var filePathWithName = directoryPath + fileName;
                                            var flat = FileSystem.ReadFile(filePathWithName);
                                            if (flat != null)
                                            {
                                                var md5 = FileSystem.CalcMd5Hash(flat);
                                                if (md5.Equals(t[1]))
                                                {
                                                    byte[] raw = new byte[flat.Length];
                                                    Buffer.BlockCopy(flat, 0, raw, 0, flat.Length);
                                                    SA.Common.FileSystem.EasyCrypt(ref raw);
                                                    byte[] buffer = new byte[raw.Length * 4];
                                                    int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                    byte[] decrypted = new byte[len];
                                                    Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                    footballerList = FootballerList_FBS.GetRootAsFootballerList_FBS(new ByteBuffer(decrypted));
                                                    int length = footballerList.DataListLength;
                                                    for (int i = 0; i < length; i++)
                                                    {

                                                        Footballer? obj = footballerList.DataList(i);
                                                        if (!footballerCache.ContainsKey(obj.Value.Rid))
                                                        {
                                                            footballerCache.Add(obj.Value.Rid, obj.Value);
                                                        }
                                                        else
                                                        {
                                                            Debugger.LogError("球员数据重复" + obj.Value.Rid);
                                                        }
                                                    }
                                                    Debugger.Log("footballerCache size:" + footballerCache.Count);
                                                    loadedCount -= 1;
                                                    continue;
                                                }
                                            }
                                        }
                                        HTTPRequest req2 = new HTTPRequest(dataUrl, HTTPMethods.Get, (req3, resp3) =>
                                        {
                                            byte[] data = resp3.Data;
                                            byte[] raw = new byte[data.Length];
                                            Buffer.BlockCopy(data, 0, raw, 0, data.Length);
                                            SA.Common.FileSystem.EasyCrypt(ref raw);
                                            byte[] buffer = new byte[raw.Length * 4];
                                            int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                            byte[] decrypted = new byte[len];
                                            Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                            footballerList = FootballerList_FBS.GetRootAsFootballerList_FBS(new ByteBuffer(decrypted));
                                            int length = footballerList.DataListLength;
                                            for (int i = 0; i < length; i++)
                                            {

                                                Footballer? obj = footballerList.DataList(i);
                                                if (!footballerCache.ContainsKey(obj.Value.Rid))
                                                {
                                                    footballerCache.Add(obj.Value.Rid, obj.Value);
                                                }
                                                else
                                                {
                                                    Debugger.LogError("球员数据重复" + obj.Value.Rid);
                                                }
                                            }
                                            Debugger.Log("footballerCache size:" + footballerCache.Count);
                                            loadedCount -= 1;
                                        });
                                        req2.Timeout = TimeSpan.FromMilliseconds(3000);
                                        req2.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                                        req2.DisableCache = true;
                                        req2.Send();
                                    }
                                    else if (fileName == "SkillList_FBS.flat")
                                    {
                                        if (fileExits)
                                        {
                                            var filePathWithName = directoryPath + fileName;
                                            var flat = FileSystem.ReadFile(filePathWithName);
                                            if (flat != null)
                                            {
                                                var md5 = FileSystem.CalcMd5Hash(flat);
                                                if (md5.Equals(t[1]))
                                                {
                                                    byte[] raw = new byte[flat.Length];
                                                    Buffer.BlockCopy(flat, 0, raw, 0, flat.Length);
                                                    SA.Common.FileSystem.EasyCrypt(ref raw);
                                                    byte[] buffer = new byte[raw.Length * 4];
                                                    int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                                    byte[] decrypted = new byte[len];
                                                    Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                                    skillList = SkillList_FBS.GetRootAsSkillList_FBS(new ByteBuffer(decrypted));
                                                    int length = skillList.DataListLength;
                                                    for (int i = 0; i < length; i++)
                                                    {
                                                        Skill? obj = skillList.DataList(i);
                                                        skillCache.Add(obj.Value.Rid, obj.Value);
                                                    }
                                                    loadedCount -= 1;
                                                    continue;
                                                }
                                            }
                                        }
                                        HTTPRequest req2 = new HTTPRequest(dataUrl, HTTPMethods.Get, (req3, resp3) =>
                                        {
                                            byte[] data = resp3.Data;
                                            byte[] raw = new byte[data.Length];
                                            Buffer.BlockCopy(data, 0, raw, 0, data.Length);
                                            SA.Common.FileSystem.EasyCrypt(ref raw);
                                            byte[] buffer = new byte[raw.Length * 4];
                                            int len = Utility.SparseArray_Decompress(raw, raw.Length, ref buffer);
                                            byte[] decrypted = new byte[len];
                                            Buffer.BlockCopy(buffer, 0, decrypted, 0, len);
                                            skillList = SkillList_FBS.GetRootAsSkillList_FBS(new ByteBuffer(decrypted));
                                            int length = skillList.DataListLength;
                                            for (int i = 0; i < length; i++)
                                            {
                                                Skill? obj = skillList.DataList(i);
                                                skillCache.Add(obj.Value.Rid, obj.Value);
                                            }
                                            Debugger.Log("skillCache size:" + skillCache.Count);
                                            loadedCount -= 1;
                                        });
                                        req2.Timeout = TimeSpan.FromMilliseconds(3000);
                                        req2.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                                        req2.DisableCache = true;
                                        req2.Send();
                                    }
                                }
                            });
                            req1.Timeout = TimeSpan.FromMilliseconds(3000);
                            req1.ConnectTimeout = TimeSpan.FromMilliseconds(3000);
                            req1.DisableCache = true;
                            req1.Send();
                        }
                        else if (serverListDTO.GameStatus == (int)GameServiceStatus.Maintain)
                        {
                            Debugger.Log("游戏维护中......");
                        }
                    }
                    else
                    {
                        //UIMgr.PopView(ViewNames.WarnTip).GetComponent<WarnPanel>().Init("StatusCode:" + endResp0.StatusCode, 3);
                        RetryFetchServerList(false);
                    }
                    break;
                case HTTPRequestStates.Error:
                    RetryFetchServerList(false);
                    break;
                case HTTPRequestStates.ConnectionTimedOut:
                    RetryFetchServerList(true);
                    break;
                case HTTPRequestStates.TimedOut:
                    RetryFetchServerList(false);
                    break;
            }
        });
        req0.ConnectTimeout = TimeSpan.FromSeconds(5);
        req0.Timeout = TimeSpan.FromSeconds(10);
        req0.DisableCache = true;
        req0.Send();
    }

    void RetryFetchServerList(bool shiftBackupUrl)
    {
        retryCount += 1;
        //if (retryCount > 3)
        //{
        //    UIMgr.PopView(ViewNames.WarnTip).GetComponent<WarnPanel>().Init("加载服务器列表 失败3次 放弃", 5);
        //}
        //else
        //{
        //    UIMgr.PopView(ViewNames.WarnTip).GetComponent<WarnPanel>().Init("加载服务器列表 失败" + retryCount + "次 正在重试...", 3);
        //    if (shiftBackupUrl)
        //    {
        //        serverListUrl = jointServerListUrl(serverListUrl2);
        //    }
        //    FetchServerList();
        //}
    }

    void Update()
    {
        if (!isCall && loadedCount == 0)
        {
            parseData();
            EventBus<StaticDataLoadSuccessEvent>.Raise(new StaticDataLoadSuccessEvent());
            isCall = true;
        }
    }

    void parseData()
    {
        HashSet<int> shortPass_Gk = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosGkShortPassLength; i++)
        {
            shortPass_Gk.Add(gameConfig.FootballerPosGkShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeGk, shortPass_Gk);
        HashSet<int> shortPass_Cf = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCfShortPassLength; i++)
        {
            shortPass_Cf.Add(gameConfig.FootballerPosCfShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeCf, shortPass_Cf);
        HashSet<int> shortPass_Lw = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosLwShortPassLength; i++)
        {
            shortPass_Lw.Add(gameConfig.FootballerPosLwShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeLw, shortPass_Lw);
        HashSet<int> shortPass_Rw = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosRwShortPassLength; i++)
        {
            shortPass_Rw.Add(gameConfig.FootballerPosRwShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeRw, shortPass_Rw);
        HashSet<int> shortPass_Cam = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCamShortPassLength; i++)
        {
            shortPass_Cam.Add(gameConfig.FootballerPosCamShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeCam, shortPass_Cam);
        HashSet<int> shortPass_Lm = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosLmShortPassLength; i++)
        {
            shortPass_Lm.Add(gameConfig.FootballerPosLmShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeLm, shortPass_Lm);
        HashSet<int> shortPass_Rm = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosRmShortPassLength; i++)
        {
            shortPass_Rm.Add(gameConfig.FootballerPosRmShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeRm, shortPass_Rm);
        HashSet<int> shortPass_Cm = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCmShortPassLength; i++)
        {
            shortPass_Cm.Add(gameConfig.FootballerPosCmShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeCm, shortPass_Cm);
        HashSet<int> shortPass_Cb = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCbShortPassLength; i++)
        {
            shortPass_Cb.Add(gameConfig.FootballerPosCbShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeCb, shortPass_Cb);
        HashSet<int> shortPass_Lb = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosLbShortPassLength; i++)
        {
            shortPass_Lb.Add(gameConfig.FootballerPosLbShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeLb, shortPass_Lb);
        HashSet<int> shortPass_Rb = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosRbShortPassLength; i++)
        {
            shortPass_Rb.Add(gameConfig.FootballerPosRbShortPass(i));
        }
        shortPassPos.Add(baseEnumConfig.FootballerPosTypeRb, shortPass_Rb);

        HashSet<int> longPass_Gk = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosGkLongPassLength; i++)
        {
            longPass_Gk.Add(gameConfig.FootballerPosGkLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeGk, longPass_Gk);
        HashSet<int> longPass_Cf = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCfLongPassLength; i++)
        {
            longPass_Cf.Add(gameConfig.FootballerPosCfLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeCf, longPass_Cf);
        HashSet<int> longPass_Lw = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosLwLongPassLength; i++)
        {
            longPass_Lw.Add(gameConfig.FootballerPosLwLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeLw, longPass_Lw);
        HashSet<int> longPass_Rw = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosRwLongPassLength; i++)
        {
            longPass_Rw.Add(gameConfig.FootballerPosRwLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeRw, longPass_Rw);
        HashSet<int> longPass_Cam = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCamLongPassLength; i++)
        {
            longPass_Cam.Add(gameConfig.FootballerPosCamLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeCam, longPass_Cam);
        HashSet<int> longPass_Lm = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosLmLongPassLength; i++)
        {
            longPass_Lm.Add(gameConfig.FootballerPosLmLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeLm, longPass_Lm);
        HashSet<int> longPass_Rm = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosRmLongPassLength; i++)
        {
            longPass_Rm.Add(gameConfig.FootballerPosRmLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeRm, longPass_Rm);
        HashSet<int> longPass_Cm = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCmLongPassLength; i++)
        {
            longPass_Cm.Add(gameConfig.FootballerPosCmLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeCm, longPass_Cm);
        HashSet<int> longPass_Cb = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosCbLongPassLength; i++)
        {
            longPass_Cb.Add(gameConfig.FootballerPosCbLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeCb, longPass_Cb);
        HashSet<int> longPass_Lb = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosLbLongPassLength; i++)
        {
            longPass_Lb.Add(gameConfig.FootballerPosLbLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeLb, longPass_Lb);
        HashSet<int> longPass_Rb = new HashSet<int>();
        for (int i = 0; i < gameConfig.FootballerPosRbLongPassLength; i++)
        {
            longPass_Rb.Add(gameConfig.FootballerPosRbLongPass(i));
        }
        longPassPos.Add(baseEnumConfig.FootballerPosTypeRb, longPass_Rb);

        //接门将球的位置
        for (int i = 0; i < gameConfig.FootballerPosGkShortPassLength; i++)
        {
            attackStartPos.Add(gameConfig.FootballerPosGkShortPass(i));
        }
        for (int i = 0; i < gameConfig.FootballerPosGkLongPassLength; i++)
        {
            attackStartPos.Add(gameConfig.FootballerPosGkLongPass(i));
        }

        //可以射门的位置
        attackShootPos.Add(gameConfig.FootballerShootPos);
        for (int i = 0; i < gameConfig.FootballerLongShootPosLength; i++)
        {
            attackShootPos.Add(gameConfig.FootballerLongShootPos(i));
        }
        for (int i = 0; i < gameConfig.FootballerSuperLongShootPosLength; i++)
        {
            attackShootPos.Add(gameConfig.FootballerSuperLongShootPos(i));
        }
        posOppPos.Add(baseEnumConfig.FootballerPosTypeGk, baseEnumConfig.FootballerPosTypeGkOppGk);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeCf, baseEnumConfig.FootballerPosTypeCfOppCb);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeLw, baseEnumConfig.FootballerPosTypeLwOppRb);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeRw, baseEnumConfig.FootballerPosTypeRwOppLb);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeCam, baseEnumConfig.FootballerPosTypeCamOppCm);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeLm, baseEnumConfig.FootballerPosTypeLmOppRm);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeRm, baseEnumConfig.FootballerPosTypeRmOppLm);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeCm, baseEnumConfig.FootballerPosTypeCmOppCam);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeCb, baseEnumConfig.FootballerPosTypeCbOppCf);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeLb, baseEnumConfig.FootballerPosTypeLbOppRw);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeRb, baseEnumConfig.FootballerPosTypeRbOppLw);
        posOppPos.Add(baseEnumConfig.FootballerPosTypeGkOppGk, baseEnumConfig.FootballerPosTypeGk);
    }

    public string titleWithRId(int rid)
    {
        int itemType = int.Parse(rid.ToString().Substring(0, 2));
        if (itemType == baseEnumConfig.ResTypeFootballer)
        {
            return footballerCache[rid].Res.Value.Title1;
        }
        else return "";
    }

    public string footballerPosTag(Footballer proto)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < proto.PosLength; i++)
        {
            string tag = "";
            if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeCf) tag = "CF";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeCf) tag = "CF";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeLw) tag = "LW";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeRw) tag = "RW";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeCam) tag = "CAM";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeLm) tag = "LM";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeRm) tag = "RM";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeCm) tag = "CM";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeCb) tag = "CB";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeLb) tag = "LB";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeRb) tag = "RB";
            else if (proto.Pos(i) == baseEnumConfig.FootballerPosTypeGk) tag = "GK";
            sb.Append(tag + " ");
        }
        return sb.ToString();
    }

    public bool checkPos(int rid, int pos)
    {
        if (footballerCache.ContainsKey(rid))
        {
            Footballer proto = footballerCache[rid];
            for (int i = 0; i < proto.PosLength; i++)
            {
                if (proto.Pos(i) == pos)
                {
                    return true;
                }
            }
            return false;
        }
        else return false;
    }
}
